package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.RecycleViewDivider;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.SystemPrintl;
import com.app.utils.TransformController;
import com.app.view.BaseViewStateLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.text.MessageFormat;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.model.CourseInterItem;
import phone.qyg.com.qyg.model.CourseInterTrans;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/13 10:50
 * 邮箱：lanbuhan@163.com
 * 功能：课程提问一级页面
 */
@ContentView(R.layout.course_inter_two)
public class CourseInterLocuTwoActivity extends BaseActivity {

    String ONE = "<font color='#ff6633'>{0}</font>";

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.linear)
    LinearLayout linear;

    @ViewInject(R.id.send_reply)
    RelativeLayout send_reply;

    BaseViewStateLayout baseView;

    RecyclerView recycler;

    Base base;

    @Override
    public void initSucessView() {
        base = (Base) getTransModels();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(null == base) return;


        baseView = new BaseViewStateLayout(QXApplication.getContext()) {

            @Override
            public View obtionView() {
                View sucessView = View.inflate(QXApplication.getContext(), R.layout.base_recycler,null);
                recycler = (RecyclerView) sucessView.findViewById(R.id.recycler);

                LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
                layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
                recycler.addItemDecoration(new RecycleViewDivider(
                        mContext,
                        LinearLayoutManager.HORIZONTAL,
                        CommonUtil.dip2px(QXApplication.getContext(),5),
                        QXApplication.getContext().getResources().getColor(R.color.no_color)));

                recycler.setLayoutManager(layoutVerManager);
                return sucessView;
            }

            @Override
            public Object obtionData() {
                initNetWork();
                return DELFAUTSTATE;
            }
        };

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linear.addView(baseView,layoutParams);
        x.view().inject(linear);
    }

    private void initNetWork(){
        mapKeys.put(AppHttpKey.ID,String.valueOf(base.id));
        AppHttpRequest.httpRequestDefalut(ApplicationConfig.SECONDANS_OF_QUES,
                mapKeys,
                new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState){
                            List<CourseInterItem> models = JSONTool.jsonDefaluTranClazzs(result,JSONTool.Enum.ANS_LIST,CourseInterItem.class);
                            setModelData(models);
                        }else{
                            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_ERROE);
                            baseView.stateView();
                        }
                    }

                });
    }



    private void setModelData(List<CourseInterItem> models) {
        if(isRequestList(models)){
            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
            baseView.stateView();
        }else{
            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
            baseView.stateView();
            return;
        }

        final CommonAdapter adapter = new CommonAdapter<CourseInterItem>(QXApplication.getContext(),R.layout.course_inter_item,models) {
            @Override
            protected void convert(ViewHolder holder, CourseInterItem model, int position) {
                holder.setText(R.id.item_address,model.dateDiff);
                holder.setText(R.id.item_summary,model.content);
                holder.setVisible(R.id.item_count,false);
                holder.setImage(R.id.item_img,model.fixImgUrl,OptionImageUtils.getPersonIcon());


                //  holder.setText(R.id.item_name,model.user_name);

                String ansName = model.ans_name;
                String userName = model.user_name;
                holder.setTextHTMLTwo(R.id.item_name,(isRequestStr(ansName) && (userName.contains(ansName)))?
                        userName.replace(ansName, MessageFormat.format(ONE,ansName)):
                        userName);
            }
        };

        recycler.setAdapter(adapter);
    }



    @Event(value = {R.id.back,R.id.send_reply})
    private void ViewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.send_reply:
                SystemPrintl.systemPrintl("二级回复类型是---->>>"+base.ans_user_type);
                CourseInterTrans trans = new CourseInterTrans(base.relp_id,base.id,base.user_id,base.ans_user_type,ApplicationConfig.COURSE_INTER_TWO);
                TransformController.transformControllerModel(QXApplication.getContext(),CourseReleaseInterlocuActivity.class,trans);
                break;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        initNetWork();
    }

    @Override
    public void initViewHandler() {}
}
